//
//  GifImageView.m
//  GIFShow
//
//  Created by lanou11air029 on 15/4/19.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "GifImageView.h"
#import <UIImageView-PlayGIF/UIImageView+PlayGIF.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <MBProgressHUD/MBProgressHUD.h>
@interface GifImageView ()
@property (nonatomic) MBProgressHUD * progress;
@end

@implementation GifImageView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFrame:frame];
    }
    return self;
}

-(void)setUrlStr:(NSString *)urlStr{
    if (_urlStr != urlStr) {
        _urlStr = urlStr;
        [self sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {

            float rate =receivedSize*1.0/expectedSize;
            if (rate > 0 && rate <1) {
                [self.progress setProgress:rate];
            }
            if(self.progress.superview != self && self.progress.progress < 0.9){
                [self addSubview:self.progress];
                [self.progress show:YES];
                self.progress.mode = MBProgressHUDModeDeterminate;
                self.progress.removeFromSuperViewOnHide = YES;
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [MBProgressHUD hideHUDForView:self animated:YES];
        }];
        
    }
}
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    if(!self.progress){
    self.progress = [[MBProgressHUD alloc]init];
    self.progress.mode = MBProgressHUDModeDeterminate;
    self.progress.removeFromSuperViewOnHide = YES;
    [self addSubview:self.progress];
        [self.progress show:YES];
    }
    NSLog(@"%@",NSStringFromCGRect(self.progress.frame));
}

@end
